package com.yixing.tech.system.service.impl;

import org.apache.commons.lang3.StringUtils;
import com.yixing.tech.common.base.*;
import com.yixing.tech.common.enums.ComparisonSymbol;
import com.yixing.tech.common.enums.ErrCode;
import com.yixing.tech.common.enums.YesOrNo;
import com.yixing.tech.system.po.WebPage;
import com.yixing.tech.system.service.IWebPageService;
import com.yixing.tech.common.utils.DataUtil;
import com.yixing.tech.common.vo.s3.WebPageVO;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.aggregation.*;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.stereotype.Service;
import org.springframework.web.util.UriUtils;

import javax.annotation.Resource;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

/**
 * @author yixing tech
 * at 2021/12/31
 * describe：
 */
@Service
public class WebPageServiceImpl implements IWebPageService {


    @Resource
    BaseMongoDao baseMongoDao;


    @Override
    public void add(WebPageVO webPageVO) throws BusinessException {
        WebPageVO condition = new WebPageVO();
        String uri = webPageVO.getUri();
        String webType = webPageVO.getType().getId();
        if ("624e5149a6b9f60c62bc2b9c".equals(webType)) {
            String uriEncode = UriUtils.encode(uri, StandardCharsets.UTF_8);
            webPageVO.setUri(uriEncode);
        }

        condition.setName(webPageVO.getName());
        condition.putComparisonSymbols("name", ComparisonSymbol.is);
        condition.setTenantId(webPageVO.getTenantId());
        if (baseMongoDao.exists(condition, WebPage.class)) {
            throw new BusinessException(ErrCode.PAGE_EXISTED);
        }
        WebPage webPage = DataUtil.o2o(webPageVO, WebPage.class);
        webPage.setId(null);
        baseMongoDao.save(webPage);
    }

    @Override
    public void batchAdd(List<WebPageVO> list) throws BusinessException {

    }

    @Override
    public WebPageVO queryById(String id) {
        if (StringUtils.isEmpty(id)) return null;
        WebPageVO vo = DataUtil.o2o(baseMongoDao.get(id, WebPage.class), WebPageVO.class);
        return vo;
    }

    @Override
    public List<WebPageVO> queryByIds(List<String> ids) {
        return null;
    }

    @Override
    public WebPageVO queryByCondition(WebPageVO webPageVO) throws BusinessException {
        return null;
    }

    @Override
    public void update(WebPageVO webPageVO) throws BusinessException {
        WebPage webPage = DataUtil.o2o(webPageVO, WebPage.class);
        baseMongoDao.update(webPage);
    }

    @Override
    public Page<WebPageVO> list(PageRow<WebPageVO> pageRow) {
        List<AggregationOperation> operations = new ArrayList<>();
        Criteria criteria = baseMongoDao.condition2Criteria(pageRow.getCondition());
        operations.add(Aggregation.match(criteria));
        BaseVO.Sort bSort = pageRow.getCondition().getSortCondition();
        try {
            Sort sort = Sort.by(Sort.Direction.fromString(bSort.getDirection()), bSort.getCode());
            SortOperation sortOperation = Aggregation.sort(sort);
            operations.add(sortOperation);
        } catch (Exception e) {
        }
        SkipOperation skipOperation = Aggregation.skip(Long.valueOf(pageRow.getPageNo() * pageRow.getPageSize()));
        operations.add(skipOperation);

        LimitOperation limitOperation = Aggregation.limit(pageRow.getPageSize());
        operations.add(limitOperation);

        operations.add(Aggregation.lookup("dict", "type._id", "_id", "type"));
        operations.add(Aggregation.unwind("type", true));
//        ConvertOperators.Convert convert = ConvertOperators.Convert.convertValue("$type._id").to(JsonSchemaObject.Type.STRING);
//        operations.add(Aggregation.addFields().addFieldWithValue("type.id", convert).build());

        List<WebPageVO> l = baseMongoDao.aggregate(operations, "webPage", WebPageVO.class).getMappedResults();
        long count = baseMongoDao.count(criteria, WebPage.class);
        return new Page<>(l, pageRow.getPageSize(), pageRow.getPageNo() + 1, count);
    }

    @Override
    public List<WebPageVO> list(WebPageVO condition) {
        List<WebPageVO> list = baseMongoDao.list(condition, WebPage.class, WebPageVO.class);
        return list;
    }


    @Override
    public void deleteById(String id) throws BusinessException {
        WebPage webPage = new WebPage();
        webPage.setId(id);
        webPage.setIsDelete(YesOrNo.YES.ordinal());
        baseMongoDao.update(webPage);
    }

    @Override
    public void delete(List<String> ids) throws BusinessException {

    }

}
